package com.wgxmkalftp.web.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.wgxmkalftp.pojo.User;
import com.wgxmkalftp.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wgx
 * @description
 * @create 2021/3/9/10:03/
 * @origin packageName:com.wgxmkalftp.usercenter.controller    projectName:PersonalityWallet
 * @since
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Reference(version = "1.0.0")
    private UserService userService;

    @PostMapping("register")
    public ResponseEntity<String> register(@RequestHeader Map<String, String> param) {
        try {
            String username = param.get("username");
            String phone = param.get("phone");
            String password = param.get("password");
            String code = param.get("code");
            String account_id = param.get("account_id");
            if (StringUtils.isBlank(code)) {
                //如果没有传入验证码，直接返回错误
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setPhone(phone);
            user.setAccount_id(Integer.valueOf(account_id));

            Boolean bool = userService.register(user, code);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @GetMapping("getCode/{phone}")
    public ResponseEntity<String> getCode(@PathVariable("phone")String phone){
        try {
            String code=userService.sendCode(phone);
            if (code!=null){
                return ResponseEntity.ok(code);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}